In this article, I will show you to calculate how many words have I typed in a textbox using javaScript. With the help of below code, when the user enters some text in a textbox it calculate word count on a JavaScript keyup event.
Example:
<script type="text/javascript">
function countwords() {
var val = document.getElementById("txtinput").value;
var words = 0, chars = 0;
if (val != "") {
words = val.replace(/\s+/gi, ' ').split(' ').length; // Count words
chars = val.length; // Count characters
}
document.getElementById("divAlert").innerHTML = words;
}
</script>
<h2>counting words in a essay</h2>
<br />
<input type="text" id="txtinput" onkeyup="countwords()" style="width: 250px; height: 30px;" /><br />
<div id="divAlert" style="text-align: left; margin-bottom: 10px; color: red">
</div>
Output:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article